// Ionic Starter App

// angular.module is a global place for creating, registering and retrieving Angular modules
// 'starter' is the name of this angular module example (also set in a <body> attribute in index.html)
// the 2nd parameter is an array of 'requires'
// 'starter.services' is found in services.js
// 'starter.controllers' is found in controllers.js
angular.module('starter', ['ionic', 'starter.controllers', 'starter.services', 'ngResource', 'ngCookies','ionicLazyLoad'])

.run(function($ionicPlatform, $rootScope,$ionicHistory,$http,cfg,$location,$anchorScroll,uploadImg,$cookies) {
  uploadImg.initWxSdk();
  $ionicPlatform.ready(function() {
    // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
    // for form inputs)
    if (window.cordova && window.cordova.plugins && window.cordova.plugins.Keyboard) {
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
      cordova.plugins.Keyboard.disableScroll(true);

    }
    if (window.StatusBar) {
      // org.apache.cordova.statusbar required
      StatusBar.styleDefault();
    }
  });

  $rootScope.setTitle = function(title){
    if(!title){
      document.title = '美术馆';
    }else{
      document.title = title;
    }
    var $body = $('body');
    // hack在微信等webview中无法修改document.title的情况
    var $iframe = $('<iframe src="" style="display:none"></iframe>').on('load', function() {
      setTimeout(function() {
        $iframe.off('load').remove()
      }, 0)
    }).appendTo($body)
  }

  $rootScope.clearHistory = function(){
    $ionicHistory.clearHistory();
  };

  $rootScope.fullPath = function(url){
    return cfg.baseImgUrl + url;
  };

  $rootScope.$on('$stateChangeStart',
  function(event, toState, toParams, fromState, fromParams, cfg){
      if(toState.hideTab){
        $rootScope.hideTab = true;
      }
      else{
        $rootScope.hideTab = false;
      }
      $rootScope.setTitle(toState.title);
  });
})

.config(function($stateProvider, $urlRouterProvider) {

  // Ionic uses AngularUI Router which uses the concept of states
  // Learn more here: https://github.com/angular-ui/ui-router
  // Set up the various states which the app can be in.
  // Each state's controller can be found in controllers.js
  $stateProvider

  // setup an abstract state for the tabs directive
  .state('tab', {
    url: '',
    abstract: true,
    templateUrl: 'templates/tabs.html'
  })

  .state('register', {
    url: '/register',
    cache: false,
    templateUrl: 'templates/register.html',
    controller: 'RegisterCtrl'
  })

  // Each tab has its own nav history stack:

  .state('tab.dash', {
    url: '/dash',
    views: {
      'tab-dash': {
        templateUrl: 'templates/tab-dash.html',
        controller: 'DashCtrl'
      }
    }
  })

  .state('tab.chats', {
      url: '/chats',
      views: {
        'tab-chats': {
          templateUrl: 'templates/tab-chats.html',
          controller: 'ChatsCtrl'
        }
      }
    })
    .state('tab.chat-detail', {
      url: '/chats/:chatId',
      hideTab: true,
      views: {
        'tab-chats': {
          templateUrl: 'templates/chat-detail.html',
          controller: 'ChatDetailCtrl'
        }
      }
    })

  .state('tab.account', {
    url: '/account',
    views: {
      'tab-account': {
        templateUrl: 'templates/tab-account.html',
        controller: 'AccountCtrl'
      }
    }
  })

  .state('tab.exhibition-detail', {
    url: '/exhibition-detail/:exhibitionId',
    hideTab: true,
    views: {
      'tab-chats': {
        templateUrl: 'templates/exhibition-detail.html',
        controller: 'ExhibitionCtrl'
      }
    }
  })

  .state('tab.home', {
    url: '/home',
    hideTab: true,
    cache:false,
    views: {
      'tab-chats': {
        templateUrl: 'templates/home.html',
        controller: 'HomeCtrl'
      }
    }
  })

  .state('tab.login', {
    url: '/login',
    hideTab: true,
    views: {
      'tab-chats': {
        templateUrl: 'templates/login.html',
        controller: 'LoginCtrl'
      }
    }
  })

  .state('tab.artlist', {
    url: '/artlist/:categoryId',
    hideTab: true,
    views: {
      'tab-chats': {
        templateUrl: 'templates/artlist.html?'+new Date(),
        controller: 'ArtlistCtrl'
      }
    }
  })

  .state('tab.bigart', {
    url: '/bigart/:id',
    hideTab: true,
    cache: false,
    views: {
      'tab-chats': {
        templateUrl: 'templates/bigart.html?'+new Date(),
        controller: 'BigartCtrl'
      }
    }
  })
  
  .state('tab.my_ticket', {
    url: '/my_ticket',
    hideTab: true,
    views: {
      'tab-chats': {
        templateUrl: 'templates/my_ticket.html',
        controller: 'MyticketCtrl'
      }
    }
  })

  .state('tab.ticket_detail', {
    url: '/ticket_detail/:id',
    hideTab: true,
    cache: false,
    views: {
      'tab-chats': {
        templateUrl: 'templates/ticket_detail.html',
        controller: 'TicketdetailCtrl'
      }
    }
  })

  .state('tab.center', {
    url: '/center',
    hideTab: true,
    views: {
      'tab-chats': {
        templateUrl: 'templates/center.html',
        controller: 'CenterCtrl'
      }
    }
  })

  .state('tab.info', {
    url: '/info',
    hideTab: true,
    views: {
      'tab-chats': {
        templateUrl: 'templates/info.html',
        controller: 'InfoCtrl'
      }
    }
  })

  .state('tab.qrcode', {
    url: '/qrcode',
    hideTab: true,
    views: {
      'tab-chats': {
        templateUrl: 'templates/qrcode.html',
        controller: 'QrcodeCtrl'
      }
    }
  })

  .state('tab.productlist', {
    url: '/productlist/:id',
    hideTab: true,
    views: {
      'tab-chats': {
        templateUrl: 'templates/productlist.html',
        controller: 'ProductlistCtrl'
      }
    }
  })

  .state('tab.activitylist', {
    url: '/activitylist/:type',
    hideTab: true,
    views: {
      'tab-chats': {
        templateUrl: 'templates/activitylist.html',
        controller: 'ActivitylistCtrl'
      }
    }
  })

  .state('tab.productdetail', {
    url: '/productdetail/:id',
    hideTab: true,
    views: {
      'tab-chats': {
        templateUrl: 'templates/productdetail.html',
        controller: 'ProductdetailCtrl'
      }
    }
  })

  .state('tab.order', {
    url: '/order',
    hideTab: true,
    cache:false,
    views: {
      'tab-chats': {
        templateUrl: 'templates/order.html',
        controller: 'OrderCtrl'
      }
    }
  })

  .state('tab.cart', {
    url: '/cart',
    hideTab: true,
    cache:false,
    views: {
      'tab-chats': {
        templateUrl: 'templates/cart.html',
        controller: 'CartCtrl'
      }
    }
  })
  
  .state('tab.checkpay', {
    url: '/checkpay/:id',
    hideTab: true,
    views: {
      'tab-chats': {
        templateUrl: 'templates/checkpay.html',
        controller: 'CheckpayCtrl'
      }
    }
  })

  .state('tab.checkvip', {
    url: '/checkvip/:type',
    hideTab: true,
    views: {
      'tab-chats': {
        templateUrl: 'templates/checkvip.html',
        controller: 'CheckvipCtrl'
      }
    }
  })

  .state('tab.placelist', {
    url: '/placelist',
    hideTab: true,
    cache: false,
    views: {
      'tab-chats': {
        templateUrl: 'templates/placelist.html',
        controller: 'PlacelistCtrl'
      }
    }
  })
  
  .state('tab.placefix', {
    url: '/placefix',
    hideTab: true,
    cache: false,
    views: {
      'tab-chats': {
        templateUrl: 'templates/placefix.html',
        controller: 'PlacefixCtrl'
      }
    }
  })

  .state('tab.placerepair', {
    url: '/placerepair/:id',
    hideTab: true,
    cache: false,
    views: {
      'tab-chats': {
        templateUrl: 'templates/placerepair.html',
        controller: 'PlacerepairCtrl'
      }
    }
  })

  .state('tab.paysuccess', {
    url: '/paysuccess',
    hideTab: true,
    cache: false,
    views: {
      'tab-chats': {
        templateUrl: 'templates/paysuccess.html',
        controller: 'PaysuccessCtrl'
      }
    }
  })

  .state('tab.vipsuccess', {
    url: '/vipsuccess',
    hideTab: true,
    cache: false,
    views: {
      'tab-chats': {
        templateUrl: 'templates/vipsuccess.html',
        controller: 'VipsuccessCtrl'
      }
    }
  })

  .state('tab.vip', {
    url: '/vip',
    hideTab: true,
    cache: false,
    views: {
      'tab-chats': {
        templateUrl: 'templates/vip.html',
        controller: 'VipCtrl'
      }
    }
  })

  .state('tab.shop', {
    url: '/shop',
    hideTab: true,
    cache: false,
    views: {
      'tab-chats': {
        templateUrl: 'templates/shop.html',
        controller: 'ShopCtrl'
      }
    }
  })

  .state('tab.wechatimgupload', {
    url: '/wechatimgupload',
    hideTab: true,
    cache: false,
    views: {
      'tab-chats': {
        templateUrl: 'templates/uploadwechatimg.html',
        controller: 'VipCtrl'
      }
    }
  });;

  // if none of the above states are matched, use this as the fallback
  $urlRouterProvider.otherwise('/home');

})
.config(function($httpProvider){
  $httpProvider.interceptors.push(function($q) {
    return {
      'responseError': function(rejection) {
        if(rejection.status == 401){
          // window.location.href = "http://store.sifangartmuseum.org.cn/home/index/index";
          window.location.href = "http://store.sifangartmuseum.org.cn/Public/museum/www/#/login";
        }
        return $q.reject(rejection);
      }
    };
  });
})
.filter('trustHtml', function ($sce) {
  return function (input) {
    return $sce.trustAsHtml(input);
  };
})

.filter('nl2br', function () {
  return function (input) {
      if (input != void 0) {
        return input.replace(/\n/g, '</\/p><p>');
      }
  };
});
